<html>
<head><meta charset="utf-8"><title>shadowing primitives · t-compiler/rust-analyzer · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/index.html">t-compiler/rust-analyzer</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/shadowing.20primitives.html">shadowing primitives</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="168163223"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/shadowing%20primitives/near/168163223" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/shadowing.20primitives.html#168163223">(Jun 14 2019 at 19:13)</a>:</h4>
<p>Hrm... so apparently, you can define a module with the same name as a primitive, and types will still resolve to that primitive: <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=7a99a7ce7b6629098b5c2512cb4737c3" target="_blank" title="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=7a99a7ce7b6629098b5c2512cb4737c3">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=7a99a7ce7b6629098b5c2512cb4737c3</a><br>
e.g. there's a <code>str</code> module in scope in liballoc's <code>string</code> module, so we don't currently resolve <code>str</code> correctly there in types</p>



<a name="168163263"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/shadowing%20primitives/near/168163263" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/shadowing.20primitives.html#168163263">(Jun 14 2019 at 19:13)</a>:</h4>
<p>in related news, I've got dot completions through <code>Arc</code>s working now, but the deref implementation of <code>String</code> still doesn't work because of the above ;)</p>



<a name="168163563"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/shadowing%20primitives/near/168163563" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/shadowing.20primitives.html#168163563">(Jun 14 2019 at 19:17)</a>:</h4>
<p>I think this worked correctly before <a href="https://github.com/rust-analyzer/rust-analyzer/issues/1352" target="_blank" title="https://github.com/rust-analyzer/rust-analyzer/issues/1352">rust-analyzer#1352</a>... shouldn't be too hard to fix though</p>



<a name="168163776"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/shadowing%20primitives/near/168163776" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/shadowing.20primitives.html#168163776">(Jun 14 2019 at 19:20)</a>:</h4>
<p>although... since you <em>can</em> apparently shadow primitives with structs, that may make it more complicated :/</p>



<a name="168163854"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/shadowing%20primitives/near/168163854" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/shadowing.20primitives.html#168163854">(Jun 14 2019 at 19:21)</a>:</h4>
<p>hm, what does <code>str::len</code> even mean if you have a <code>str</code> module?</p>



<a name="168163925"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/shadowing%20primitives/near/168163925" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/shadowing.20primitives.html#168163925">(Jun 14 2019 at 19:22)</a>:</h4>
<p>perhaps shadowing <strong>is</strong> working, but some other missing bit (like <code>pub</code>) in nares accidently exports shadowed name?</p>



<a name="168164097"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/shadowing%20primitives/near/168164097" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/shadowing.20primitives.html#168164097">(Jun 14 2019 at 19:24)</a>:</h4>
<p>this basically makes no sense: <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=19456b91117b936d251da0925c3b687b" target="_blank" title="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=19456b91117b936d251da0925c3b687b">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=19456b91117b936d251da0925c3b687b</a></p>



<a name="168164260"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/shadowing%20primitives/near/168164260" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/shadowing.20primitives.html#168164260">(Jun 14 2019 at 19:26)</a>:</h4>
<p>wait, it's even worse than that: <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=0117a00d98189990a2bd6efaa561f369" target="_blank" title="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=0117a00d98189990a2bd6efaa561f369">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=0117a00d98189990a2bd6efaa561f369</a></p>



<a name="168165104"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/shadowing%20primitives/near/168165104" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/shadowing.20primitives.html#168165104">(Jun 14 2019 at 19:37)</a>:</h4>
<p>I'm scared to look up in rustc how this works</p>



<a name="168165182"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/shadowing%20primitives/near/168165182" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/shadowing.20primitives.html#168165182">(Jun 14 2019 at 19:38)</a>:</h4>
<p>I guess, I'll be spelling <code>str</code> as <code>&lt;str&gt;</code> from now on :-)</p>



<a name="168165266"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/shadowing%20primitives/near/168165266" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/shadowing.20primitives.html#168165266">(Jun 14 2019 at 19:39)</a>:</h4>
<p>still, it might be better to err on the side of preferring the primitive instead of the module...</p>



<a name="168165380"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/shadowing%20primitives/near/168165380" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/shadowing.20primitives.html#168165380">(Jun 14 2019 at 19:40)</a>:</h4>
<p>that's true, yeah. Basically, we should stick the primiteves map in front, and comment-out the shadowing test</p>



<a name="168165444"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/shadowing%20primitives/near/168165444" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/shadowing.20primitives.html#168165444">(Jun 14 2019 at 19:41)</a>:</h4>
<p>hm, not, I fear we need to handles this at least somewhat properly: std has str and i32 modules</p>



<a name="168165630"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/shadowing%20primitives/near/168165630" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/shadowing.20primitives.html#168165630">(Jun 14 2019 at 19:43)</a>:</h4>
<p>I don't know how much resolving them correctly within std matters though... especially since they're not a problem if they're referred to by full name</p>



<a name="168165732"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/shadowing%20primitives/near/168165732" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/shadowing.20primitives.html#168165732">(Jun 14 2019 at 19:44)</a>:</h4>
<p>I <em>think</em> if you just move primites in front, the no longer will be resolved by a full nam</p>



<a name="168166558"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/shadowing%20primitives/near/168166558" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/shadowing.20primitives.html#168166558">(Jun 14 2019 at 19:54)</a>:</h4>
<p>submitted <a href="https://github.com/dtolnay/rust-quiz/pull/20/files" target="_blank" title="https://github.com/dtolnay/rust-quiz/pull/20/files">https://github.com/dtolnay/rust-quiz/pull/20/files</a> :D</p>



<a name="168167511"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/shadowing%20primitives/near/168167511" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Pascal <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/shadowing.20primitives.html#168167511">(Jun 14 2019 at 20:06)</a>:</h4>
<p>haha, that's pretty weird. i'm surprised it's able to find <code>is_empty</code>! (i guess in <code>is_empty</code> is just calls <code>s.len()</code> or <code>&lt;s as str&gt;::len()</code> so it uses the actual type of s not the mod)</p>



<a name="168175709"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/shadowing%20primitives/near/168175709" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Vadim Petrochenkov <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/shadowing.20primitives.html#168175709">(Jun 14 2019 at 22:10)</a>:</h4>
<p>Sigh, this is a backward compatibility thing.<br>
See the comment in <a href="https://github.com/rust-lang/rust/pull/32131/files" target="_blank" title="https://github.com/rust-lang/rust/pull/32131/files">https://github.com/rust-lang/rust/pull/32131/files</a> for how the fallback to primitive types work.</p>



<a name="168206643"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/shadowing%20primitives/near/168206643" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/shadowing.20primitives.html#168206643">(Jun 15 2019 at 12:46)</a>:</h4>
<p>I like how Go (I <em>think</em>) dodges this issue. They basically have an <code>#[lang = "i32"] existential type i32;</code> somewhere in the standard library, which should guarantee the same code-path in name resolution. That is, I know they have primitives declared in the stdlib, I don't know if this is what is actually used by name resolution.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>